var app = getApp();
var util = require("../../utils/util.js");
var siteinfo = require('../../siteinfo.js');
Page({

  data: {
    images: '/image/touxiang.png', //照片上传
    loginModal: false,
    isLogin: true, //是否登录
    name: '',
    idCard: '',
    schoolName: '',
    state: '', //（0待审核，1待缴费，2未通过，3已分配，4已缴费，9已删除
    isName: false,
    isIdNumber: false,
    isOrgName: false,
    index: 0,
    orgId: ''
  },

  onLoad: function (options) {
    this.setData({
      id: options.id, //认证批次id
      name: options.name,
      idCard: options.idCard,
      orgId: options.orgId,
      sigUpId: options.sigUpId,
      type: options.type
    })
    this.getUserDetail()
  },

  onShow: function () {
    var _this = this
    if (app.globalData.btn) {
      _this.getUserDetail()
      app.globalData.btn = false
    }

  },

  /**
   * 获取当前用户信息
   */
  getUserDetail: function () {
    var _this = this;
    app._getLogin(siteinfo.selUserDetail, {}, function (result) {
      if (result.data.headPortrait != '' && result.data.headPortrait != null) {
        _this.setData({
          images: result.data.headPortrait
        });
      }

      _this.setData({
        photo: result.data.headPortrait,
      });
    });
  },

  chooseImage(e) {
    var _this = this
    wx.chooseImage({
      sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        if (res.tempFiles[0].size < 1048576) {
          wx.getImageInfo({
            src: res.tempFilePaths[0],
            success(result) {
              if (result.width == '295' && result.height == '413') {
                const tempFilePaths = res.tempFilePaths
                app._uploadFile(siteinfo.fileImprot,
                  res.tempFiles[0].path,
                  'file', {
                    name: res.tempFiles[0].name
                  },
                  function (res1) {
                    _this.setData({
                      photo: res1.data,
                      images: tempFilePaths,
                    })
                  })
              } else {
                app.showError('上传的图片必须是295像素*413像素的寸照！')
              }
            }
          })
        } else {
          app.showError('寸照应小于1M！')
        }
      }
    })
  },

  //下一步
  next: function () {
    let _this = this
    if ('/image/s.png' == _this.data.photo || '' == _this.data.photo) {
      app.showError('寸照不能为空');
      return false;
    }
    app._post_form(siteinfo.editCertificateFill, {
      cbatchId: _this.data.id,
      name: _this.data.name,
      idNumber: _this.data.idCard,
      orgId: _this.data.orgId,
      photo: _this.data.photo,
      stuSignUpId: _this.data.sigUpId,
      type: _this.data.type
    },
      function (res) {
        if (res.code == '40013' || res.code == 40013) {
          //  console.log("res11", res);
          wx.showModal({
            title: '友情提示',
            content: res.msg + '可查看报名信息',
            confirmText: '去查看',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/signExamine/signExamine?id=' + _this.data.id,
                })
              }
            }
          })
        } else {
          var isOpenId = wx.getStorageSync('isOpenId')
          if (isOpenId) {
            if (isOpenId == true) {
              app.subscribeMessage(res.data.tempId)
            }
          }
          wx.redirectTo({
            url: '/pages/signExamine/signExamine?id=' + _this.data.id,
          })
        }
      }
    );
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})